# coding=utf-8

import web
from web.utils import storage

import settings
import utils

from controllers.users import UserSession
from controllers.users.sc import SC

import models

render = settings.RENDER

result_texts = ('经过XBT旅行性格测试，我属于乐活旅行派：喜欢说走就走，快意人生！有兴趣也来测测 http://%s/test/uYk' % settings.CONFIG.DOMAIN, 
        '经过XBT旅行性格测试，我属于乐活旅行派：喜欢说走就走，快意人生！有兴趣也来测测 http://%s/test/uYk' % settings.CONFIG.DOMAIN, )

def login(func):
    def proxy_func(iself, *args, **kw):
        if UserSession.current().logined:
            return func(iself, *args, **kw)
        else:
            raise web.seeother('/test/uYk')
    return proxy_func

class uYk_success(object):
    @login
    def GET(self, id):
        return render.test.success()

    @login
    def POST(self, id):
        session = UserSession.current()
        id = int(id)

        post = models.Test.get(id)
        if post and post.uid == session.uid:
            # 尝试获取客户端发送上来的信息
            ipt = web.input(sub = '0')
            if ipt.sub == '1':
                # 如果是来自订阅的提交
                return self.subscribe_post(session, post)

            # 发送微博
            if session.client.upload_status(status = result_texts[post.result]
                    , pic = 'result%s.jpg' % post.result):
                # 微博发送成功
                # 关注微博
                # 是否已经关注了
                if not session.client.followed():
                    # 未关注才进行关注
                    session.client.follow()
                # 显示成功信息
                return render.test.success(send_success = True, tour = post)
            else:
                # 微博发送失败
                return 'Failure!!!'

        else:
            pass

    def subscribe_post(self, session, post):
        '订阅提交'
        # 获取email地址
        ipt = web.input(email = False)
        # email如果无效
        if ipt.email:
            # 更新email
            if models.Test.update_email(post.id, ipt.email):
                # 订阅成功
                return render.test.success(sub_success = True)
        # 各种订阅失败
        return render.test.success(send_success = True, tour = post)
        


class uYk(object):
    '旅行测试'
    def GET(self, step = '0'):
        step = int(step)
        if step == 0:
            # 测试第一步
            return self.uYk_start()
        elif step == 2:
            # 测试第二步
            return self.uYk_step_2()
        pass

    def POST(self, step = '0'):
        step = int(step)
        if step == 2:
            return self.uYk_step_2_POST()
        pass
    
    def uYk_step_2_POST(self):
        session = UserSession.current()

        post = web.input()
        
        rg = [post['RadioGroup%s' % (i + 1)] for i in xrange(5)]

        rs1 = 0

        if rg[1] == '0' or rg[1] == '1':
            rs1 += 1

        if rg[2] == '0':
            rs1 += 1

        if rg[3] == '0':
            rs1 += 1
        
        result = 0
        if rs1 < 2:
            result = 1

        row = storage(uid = session.uid
                , selections = ','.join(rg)
                , plan_dest = post.plan_dest
                #, email = post.email
                , email = ''
                , result = result)

        id = models.Test.add(row)
        if id > 0:
            # 添加成功
            return web.seeother('/test/uYk/success/%s' % id)
        
        return web.seeother('/test/uYk/2')
        


        

    @login
    def uYk_step_2(self):
        '测试第二步'
        return render.test.index2()

    def uYk_start(self):
        '测试开始'
        session = UserSession.current()
        # 用户登录认证地址
        next_url = '/test/uYk/2'
        if not session.logined:
            # 如果没有登录
            # 测试使用新浪微博登录
            # 设置新浪微博的回调地址
            sc = SC()
            sc.Sina.callback_url = '/test/uYk'
            # 获取是否有登录回调参数
            query = web.input(code = False)
            if query.code:
                # 如果是验证后返回
                # 这里进行登录操作，记录各种需要记入cookie的信息
                if session.oauth_save_login(query.code, sc.Sina):
                    # 如果登录成功直接跳转到测试第二步
                    return web.seeother('/test/uYk/2')

            # 未登录或登录失败
            # 设置新浪微博的登录验证地址
            next_url = sc.Sina.get_authorize_url()

        return render.test.index(next_url = next_url)
